package com.jdd;

import cn.hutool.core.collection.CollectionUtil;
import com.jdd.common.api.vo.Result;
import com.jdd.common.constant.JddConstant;
import com.jdd.common.constant.LedConstant;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.modules.parking.entity.ConfigParkInfo;
import com.jdd.modules.parking.entity.ParkArmInfo;
import com.jdd.modules.parking.entity.ParkWorkStation;
import com.jdd.modules.parking.service.IConfigParkInfoService;
import com.jdd.modules.parking.service.IParkArmInfoService;
import com.jdd.modules.parking.service.IParkWorkStationService;
import com.jdd.modules.sdk.SdkCallInterface;
import com.jdd.modules.sdk.config.CameraSdk;
import com.jdd.modules.sdk.sdkidentifycallbackresults.ZhenShiSdkIdentifyCallbackResults;
import com.jdd.modules.sdk.utils.*;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @version 1.0
 * @Description:
 * @author： jdd尚岳
 * @Date: 2021-07-29 18:14
 */
@Component
@Slf4j
public class ParkingSingleInitApplication implements ApplicationRunner {
    @Resource
    private IConfigParkInfoService configParkInfoService;
    @Resource
    private IParkArmInfoService parkArmInfoService;
    @Resource
    private CameraSdk cameraSdk;
    @Resource
    private SdkCallInterface sdkCallInterface;
    @Resource
    private IParkWorkStationService parkWorkStationService;
    @Value("${parkInfo.parkCode}")
    private String parkCode;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (true) {
            return;
        }
        SnowFlakeUtil.getInstance();
        String id = SnowFlakeUtil.getId();


        List<ParkArmInfo> parkArmInfos = parkArmInfoService.list();
        ConfigParkInfo configParkInfo = configParkInfoService.findConfigParkInfoByParkCode();
        //将lib-dll文件随着jar解压出来
        boolean decompressResult = JarDecompressUtils.jarDecompressForDrive();
        //各个相机开始初始化


        if (null == configParkInfo) {
            log.error("车场配置信息为空！");
            return;
        }
        //获取相机可续
        String cameraType = configParkInfo.getCameraType();
        if (StringUtils.isNotEmpty(cameraType)) {
            if (cameraType.equals(JddConstant.CameraControl.CAMERA_ZHENSHI)) {
                ZhenShiSdkInit();
            }
            if (cameraType.equals(JddConstant.CameraControl.CAMERA_QIANYI)) {
                QianYiSdkInit();
            }
            if (cameraType.equals(JddConstant.CameraControl.CAMERA_HUAXIA)) {
                //芊熠相机只在Windows上初始化
                HuaXiaSdkInit();
            }
        }
        if (CollectionUtil.isEmpty(parkArmInfos)) {
            log.error("车道配置信息为空！");
            return;
        }
        for (ParkArmInfo parkArmInfo : parkArmInfos) {
            sdkCallInterface.CameraSdkInit(parkArmInfo.getIpAddr(), configParkInfo);
            //需求：如果系统是菲力德控制板，则项目启动时同步一次即可
            //如果是菲力德的控制板，则同步下时间
            try {
                if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
                    Result result = sdkCallInterface.calibrationAll(parkArmInfo.getSerialNo());
                    if (result.isSuccess()) {
                        log.info("菲力德同步时间成功");
                    }
                    // 车道为入口车道
                    if (StringUtils.isNotEmpty(parkArmInfo.getTargetAreaId()) && !"".equals(parkArmInfo.getTargetAreaId())) {
                        sdkCallInterface.Sdk485TestContentCommunication(parkArmInfo.getSerialNo(), "JD停车", LedConstant.feilideLongOneLineZero);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "JD停车", LedConstant.feilideTextOneLine);
                        sdkCallInterface.Sdk485TestContentCommunicationAll(parkArmInfo.getSerialNo(), "`C`Y年`M月`D日星期`V", LedConstant.feilideLongTwoLineOne);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "`C`Y年`M月`D日星期`V", LedConstant.feilideTextTwoLineOne);
                        sdkCallInterface.Sdk485TestContentCommunicationAll(parkArmInfo.getSerialNo(), "`H:`N:`S", LedConstant.feilideLongThreeLineZero);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "`H:`N:`S", LedConstant.feilideTextThreeLineZero);
                    } else {
                        sdkCallInterface.Sdk485TestContentCommunicationAll(parkArmInfo.getSerialNo(), "`C`Y年`M月`D日星期`V", LedConstant.feilideLongTwoLineOne);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "`C`Y年`M月`D日星期`V", LedConstant.feilideTextTwoLineOne);
                        sdkCallInterface.Sdk485TestContentCommunicationAll(parkArmInfo.getSerialNo(), "`H:`N:`S", LedConstant.feilideLongThreeLineZero);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "`H:`N:`S", LedConstant.feilideTextThreeLineZero);
                        sdkCallInterface.Sdk485TestContentCommunicationAll(parkArmInfo.getSerialNo(), "JD停车", LedConstant.feilideLongOneLineZero);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "JD停车", LedConstant.feilideTextOneLine);
                        sdkCallInterface.Sdk485TestContentCommunicationAll(parkArmInfo.getSerialNo(), "智慧出行", LedConstant.feilideLongFourLineZero);
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "智慧出行", LedConstant.feilideTextFourLineZero);
                    }
                }
                if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                    if (StringUtils.isNotEmpty(parkArmInfo.getTargetAreaId()) && !"".equals(parkArmInfo.getTargetAreaId())) {
                        sdkCallInterface.Sdk485LedModifyDatePosition(parkArmInfo.getSerialNo(), "AA55006400F60003030200EED5AF");
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "JD停车", "010100");
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "欢迎光临", "020100");
                    } else {
                        sdkCallInterface.Sdk485LedModifyDatePosition(parkArmInfo.getSerialNo(), "AA55006400F60003040300A67BAF");
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "JD停车", "010100");
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "智慧出行", "020100");
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "一车一杆", "030100");
                    }
                }
                if (JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                    if (StringUtils.isNotEmpty(parkArmInfo.getTargetAreaId()) && !"".equals(parkArmInfo.getTargetAreaId())) {
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "JD停车", "020100");
                    } else {
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "智慧出行", "010200");
                        sdkCallInterface.Sdk485LedAdvertising(parkArmInfo.getSerialNo(), "JD停车", "020100");
                    }
                }
            } catch (Exception e) {
                log.error("菲力德控制板同步时间出现问题");
            }
        }

        List<ParkWorkStation> list = parkWorkStationService.lambdaQuery().eq(ParkWorkStation::getStatus, "1").list();
        if (CollectionUtil.isNotEmpty(list)) {
            list.stream().forEach(var -> {
                var.setStatus("0");
                parkWorkStationService.updateById(var);
            });
        }
    }

    /**
     * @author lcy
     * @date 2021/8/13
     * @Description 华夏相机初始化
     */
    private void HuaXiaSdkInit() {
        log.info("===========项目启动加载华夏相机驱动并进行全局初始化===========");
        String windowsPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator + "win64" + File.separator + "ice_ipcsdk.dll";
        String linuxPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator + "linux" + File.separator + "libice_ipcsdk.so";
        String path = System.getProperty("os.name").contains("Windows") ? windowsPath : linuxPath;
        HUAXIASDK jnaLib = (HUAXIASDK) Native.loadLibrary(path, HUAXIASDK.class);
        cameraSdk.setHuaxiasdk(jnaLib);
        cameraSdk.getHuaxiasdk().ICE_IPCSDK_Init();

    }

    /**
     * @param
     * @return
     * @Description 臻识相机初始化驱动
     * @author shangyue
     * @Date 2021-07-30
     */

    private void ZhenShiSdkInit() {
        log.info("===========项目启动加载臻识相机驱动并进行全局初始化===========");
        //臻识相机加载DLL库
        String windowsPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator + "win64" + File.separator + "VzLPRSDK.dll";
        String linuxPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator + "linux" + File.separator + "libVzLPRSDK.so";
        String path = System.getProperty("os.name").contains("Windows") ? windowsPath : linuxPath;
        ZHENSHISDK INSTANCE = (ZHENSHISDK) Native.loadLibrary(path, ZHENSHISDK.class);
        cameraSdk.setZhenshiSdk(INSTANCE);
        //sdk全局初始化
        cameraSdk.getZhenshiSdk().VzLPRClient_Setup();
    }

    /**
     * @param
     * @return
     * @Description 芊熠相机初始化驱动
     * @author shangyue
     * @Date 2021-07-30
     */

    private void QianYiSdkInit() {
        log.info("===========项目启动加载芊熠相机驱动并进行全局初始化===========");
        //臻识相机加载DLL库
        /**加载SDK动态库*/
        String windowsPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator + "win64" + File.separator + "NetSDK.dll";
        String linuxPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator + "linux" + File.separator + "libnetsdk.so";
        String path = System.getProperty("os.name").contains("Windows") ? windowsPath : linuxPath;
        QIANYISDK INSTANCE = (QIANYISDK) Native.loadLibrary(path, QIANYISDK.class);

        cameraSdk.setQianYiSdk(INSTANCE);
        //sdk全局初始化
        cameraSdk.getQianYiSdk().Net_Init();
    }


}
